package com.qianya.system.face.web.patient.controller;

import cn.hutool.core.util.StrUtil;
import com.qianya.model.common.ResultCode;
import com.qianya.model.pojo.patient.qo.*;
import com.qianya.model.pojo.patient.vo.PatientFileInfoPlanVO;
import com.qianya.model.pojo.sys.vo.SysParamVO;
import com.qianya.system.face.config.RedisUtil;
import com.qianya.system.face.web.patient.model.PatientFileInfo;
import com.qianya.system.face.web.patient.service.FaceEngineService;
import com.qianya.system.face.web.patient.service.IPatientFileInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianya.util.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.qianya.model.common.CommonResult;
import com.qianya.model.pojo.patient.vo.PatientFileInfoVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* <p>
    * 患者档案 前端控制器
    * </p>
*
* @author lhl
* @since 2020-07-02
*/
@RestController
@RequestMapping("/patient/patient-file-info")
@Api(value = "PatientFileInfoController", tags = "患者档案接口")
@Slf4j
@Validated
public class PatientFileInfoController {

    @Resource
    private IPatientFileInfoService patientFileInfoService;

    @PutMapping(value = "/update")
    @ApiOperation("修改患者头像")
    public CommonResult update(@Valid @RequestBody PatientFileInfoUpdateQO patientFileInfoUpdateQO, HttpServletRequest request){
        PatientFileInfo patientFileInfo = new PatientFileInfo();
        BeanUtils.copyProperties(patientFileInfoUpdateQO,patientFileInfo);

        return patientFileInfoService.renewal(patientFileInfo, request);
    }
   /* @PostMapping(value = "/save")
    @ApiOperation("添加患者档案")
    public CommonResult save(@RequestBody  PatientFileInfoQO patientFileInfoQO,HttpServletRequest request){
        PatientFileInfo patientFileInfo = new PatientFileInfo();
        BeanUtils.copyProperties(patientFileInfoQO,patientFileInfo);
        return patientFileInfoService.create(patientFileInfo,request);
    }*/
    @ApiOperation("查找患者档案")
    @GetMapping(value = "/find")
    public CommonResult<IPage<PatientFileInfo>> find(@Valid PatientFileInfoQO patientFileInfoQO){
        return patientFileInfoService.find(patientFileInfoQO);
    }

    @ApiOperation("患者档案同步")
    @PutMapping(value = "/infoSync")
    public CommonResult infoSync(@ApiParam(value = "his住院号",required = true)
                                                               @NotBlank(message = "his住院号不能为空")
                                                               @RequestParam(defaultValue = "",name = "fileHospitalNum")String fileHospitalNum){
        return CommonResult.error(ResultCode.ERROR,"暂无法同步");
       // return patientFileInfoService.infoSync(fileHospitalNum);
    }
    @ApiOperation("患者信息查询")
    @GetMapping(value = "/view")
    public CommonResult<PatientFileInfo> view(@ApiParam(value = "患者编号",required = true)
                                                               @NotBlank(message = "患者编号不能为空")
                                                               @RequestParam(defaultValue = "",name = "fileId")String fileId){
        return patientFileInfoService.view(fileId);
    }
    @ApiOperation("发药进度")
    @GetMapping(value = "/plan")
    public CommonResult<PatientFileInfoPlanVO> plan(){
        return patientFileInfoService.plan();
    }
    @ApiOperation("根据日期查询患者服药记录")
    @GetMapping(value = "/dateRecode")
    public CommonResult<PatientFileInfoPlanVO> dateRecode(PatientOxygenRecodeQO  patientOxygenRecodeQO){

        if (StrUtil.isBlank(patientOxygenRecodeQO.getTime())){
            patientOxygenRecodeQO.setTime("");
        }
        if (!"".equals(patientOxygenRecodeQO.getTime()) && !DateUtils.valiDateTimeWithLongFormat(patientOxygenRecodeQO.getTime())){
            return CommonResult.error(ResultCode.ERROR,"日期格式不对");
        }
        return patientFileInfoService.dateRecode(patientOxygenRecodeQO);
    }
    @ApiOperation("患者类型")
    @GetMapping(value = "/getType")
    public CommonResult getType(){

            List<SysParamVO> list= (List<SysParamVO>)RedisUtil.get("sysParam");

        List<SysParamVO> wardList = new ArrayList<>();
        List<SysParamVO> courtyardList = new ArrayList<>();
        List<SysParamVO> officeList = new ArrayList<>();


            for (int i = 0; i <list.size() ; i++) {
                switch (list.get(i).getPaType()){
                    case "ward":
                        wardList.add(list.get(i));
                        break;
                    case "courtyard":
                        courtyardList.add(list.get(i));
                        break;
                    case "office":
                        officeList.add(list.get(i));
                        break;
                    default:break;

                }

            }
        Map<String,List<SysParamVO>> objectObjectHashMap = new HashMap<>();

            objectObjectHashMap.put("ward",wardList);
            objectObjectHashMap.put("courtyard",courtyardList);
            objectObjectHashMap.put("office",officeList);


        return CommonResult.success(objectObjectHashMap);
    }

}
